<template>
  <div class="app-container">
    <el-form inline>
      <el-form-item label="  "> </el-form-item>
    </el-form>
    <el-table :data="tableData" border stripe>
      <el-table-column type="index" align="center" width="50"></el-table-column>
      <el-table-column
        label="实例名称"
        prop="name"
        align="center"
      ></el-table-column>
      <el-table-column
        label="业务ID"
        prop="businessKey"
        align="center"
      ></el-table-column>
      <el-table-column
        label="流程名称"
        prop="processDefinitionVO.name"
        align="center"
      ></el-table-column>
      <el-table-column
        label="流程版本"
        prop="processDefinitionVO.version"
        align="center"
      ></el-table-column>
      <el-table-column
        label="流程状态"
        prop="status"
        align="center"
      ></el-table-column>
      <el-table-column
        label="开始时间"
        prop="startDate"
        align="center"
      ></el-table-column>
      <el-table-column label="基础操作" align="center">
        <template slot-scope="scope">
          <el-button type="text" size="medium" v-if="scope.row.status == 'RUNNING'" @click="suspend(scope.row)">挂起</el-button>
          <el-button type="text" size="medium" v-if="scope.row.status == 'SUSPENDED'" @click="resume(scope.row)">激活</el-button>
          <el-button type="text" size="medium" @click="deleteInstance(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: []
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.$api.getInstances().then((res) => {
        this.tableData = res.data;
      });
    },
    startProcessInstance(processDefined) {
      let key = processDefined.key;
      this.$prompt("请输入流程示例名称", "准备部署", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      })
        .then(({ value }) => {
          this.$message({
            type: "success",
            message: "正在部署中...",
          });
          let instanceName = value;
          this.$api.startProcessInstance(key, instanceName).then((res) => {
            console.log(res);
          });
        })
        .catch(() => {});
    },
    deleteInstance(instance) {
      this.$confirm("此操作将永久删除该流程实例, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$api.deleteInstance(instance.id).then((res) => {
            if (res.status == 0) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.getList();
            } else {
              this.$message({
                type: "error",
                message: res.msg,
              });
            }
          });
        })
        .catch(() => {});
    },
    suspend(instance){
      this.$api.suspendedInstance(instance.id).then(res=>{
          this.getList()
      })
    },
    resume(instance){
      this.$api.resumeInstance(instance.id).then(res=>{
        this.getList()
      })
    }
  },
};
</script>
